'######################################################################################
'Author  :         Sumit Gupta 
'Company :				 Vikasumit
'Date    :         16th August 2005
'--------------------------------------------------------------------------------------
'Working / Note on File : 
'Contains the Game Specific Functions
'######################################################################################

Imports System.Net
Imports System.IO

Module ModFunctions

  'Online Server Will Note the following Things
  'Name, IP Address, Date, Time, Port, Http Referer,Chess Version

  'Data to Send through links are chessversion,Name, Port
  'Links
  Dim tmpboard As VSChessEngine.clsGameEngine

  Public ChessVersion As String

  Sub DeclareParameters()
    tmpboard = New VSChessEngine.clsGameEngine
    ChessVersion = tmpboard.ClassVersion
    tmpboard = Nothing
  End Sub

  'Function to return the numeric Equivalent of AlphaBet used in Chess Game
  Function ConvertStringtoInt(ByVal param As String) As Integer
    Select Case param
      Case "a"
        Return 0
      Case "b"
        Return 1
      Case "c"
        Return 2
      Case "d"
        Return 3
      Case "e"
        Return 4
      Case "f"
        Return 5
      Case "g"
        Return 6
      Case "h"
        Return 7
      Case Else
        Return 0
    End Select
  End Function

  Sub MakeMeOnline(ByVal Name As String)
    Try
      Dim xlog As New VSUtility.clsLog("Web Request", "1")
      DeclareParameters()
      Dim webaddress As String = OnlineLink & "?name=" & Name & "&chessversion=" & ChessVersion & _
      "&port=" & AppConfig.GetValue("Settings", "Port")
      Dim request As HttpWebRequest = CType(WebRequest.Create(webaddress), HttpWebRequest)
      request.UserAgent = HTTPReferrer
      Dim res1 As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
      Dim str As Stream = res1.GetResponseStream
      Dim sr As New StreamReader(str)
      Dim output As String = ""
      output = sr.ReadToEnd()
      res1.Close()
    Catch ex As Exception
      MsgBox("Some error has occur when trying to set Network Game.")
    End Try
  End Sub

  Sub MakeMeOffline(ByVal Name As String)
    Try
      DeclareParameters()
      Dim webaddress As String = OfflineLink & "?name=" & Name
      Dim request As HttpWebRequest = CType(WebRequest.Create(webaddress), HttpWebRequest)
      request.UserAgent = HTTPReferrer
      Dim res1 As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
      res1.Close()
    Catch
      MsgBox("Fail to log you out of network. Please email us on support@vikasumit.com")
    End Try
  End Sub
End Module
